<template>
  <el-dialog
      :title="!dataForm.id ? '新增' : '修改'"
      :close-on-click-modal="false"
      :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
      <el-form-item label="资源名" prop="name">
        <el-input v-model="dataForm.name" placeholder="资源名"></el-input>
      </el-form-item>
      <el-form-item label="文档类型" prop="type">
        <el-select v-model="dataForm.type" placeholder="请选择">
          <el-option
              placeholder="请选择资源类型"
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="资源上传" prop="name">
        <el-upload
            class="upload-demo"
            :action="uploadUrl"
            :limit="1"
            :on-success="handleSuccess"
            :on-error="handleError">
          <el-button slot="trigger" size="small" type="primary">源数据上传</el-button>
        </el-upload>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" v-if="canSubmit">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import {getObj, addObj, putObj} from '@/api/teacher/resource'
import store from "@/store";

export default {
  data() {
    return {
      uploadUrl: 'http://localhost:9528/dev-api/file/upload',
      options: [{
        value: '1',
        label: '视频'
      }, {
        value: '2',
        label: '文档'
      }],
      visible: false,
      canSubmit: false,
      dataForm: {
        id: 0,
        type: '',
        name: '',
        url: '',
        status: '',
        createTime: '',
        updateTime: ''
      },
      dataRule: {
        type: [
          {
            required: true,
            message: '资源类别，例如：视频、文档(video, document)，视频限定后缀为mp3，文档为pdf, doc, docx, md, txt不能为空',
            trigger: 'blur'
          }
        ],
        name: [
          {required: true, message: '资源名不能为空', trigger: 'blur'}
        ],
        url: [
          {required: true, message: 'URL不能为空', trigger: 'blur'}
        ],
        status: [
          {required: true, message: '资源状态，0-待审核、1-审核失败、2-审核通过(已上架)、3-下架不能为空', trigger: 'blur'}
        ],
        createTime: [
          {required: true, message: '创建时间不能为空', trigger: 'blur'}
        ],
        updateTime: [
          {required: true, message: '更新时间不能为空', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.canSubmit = true;
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          getObj(this.dataForm.id).then(response => {
            this.dataForm = response.data
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.canSubmit = false;
          if (this.dataForm.id) {
            putObj(this.dataForm).then(data => {
              this.$notify.success('修改成功')
              this.visible = false
              this.$emit('refreshDataList')
            }).catch(() => {
              this.canSubmit = true;
            });
          } else {
            addObj(this.dataForm).then(data => {
              this.$notify.success('添加成功')
              this.visible = false
              this.$emit('refreshDataList')
            }).catch(() => {
              this.canSubmit = true;
            });
          }
        }
      })
    },
    handleSuccess(response, file, fileList) {
      this.dataForm.url = response.data
      this.dataForm.userId = store.getters.token.split(",")[2]
    },
    handleError(err, file, fileList) {
      console.error('Error during upload:', err);
    }
  }
}
</script>
